home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Painted into a corner?
- Date: Sun, 04 Feb 1996 23:16:13 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4f3en2$7jf@news.halcyon.com>
- References: <d5e2.smail.smayo@tiac.net>
- NNTP-Posting-Host: blv-pm11-ip19.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- smayo@tiac.net (Scott Mayo) wrote:
-
- >I've stumbled into an awkward case ...
-
- >I have a class, X, which is never going to be instantiated, but subclasses derived
- >from it will be. I have another class, Y, which might be instantiated someday,
- >though at the moment it exists only to support the subclasses Y1, Y2, and Y3.
-
- >I need to create 3 new classes, X1, X2, and X3, which inherit as follows
- >X1: X and Y1
- >X2: X and Y2
- >X3: X and Y3
- >X4: X
-
- >...I always need to manipulate instances of
- >Xn through pointers to X, and trust to virtual functions
- >to get the properly X1ish, X2ish and X3ish behaviour I need as regards
- >the behaviour of the Yish parts. X4 is willing to support dummy functions
- >so that it looks Yish as well; I'd rather it didn't inherit Y directly
- >as that would make it pointlessly bulky.
-
- >The problem, which the experienced folk here probably see already, is that
- >if I create X *x, any time I try to do something like x->member_of_y(),
- >I get told that member_of_y() isn't part of X. ...
-
- >The closest I've come to a workaround is to create, in X,
- >virtual member_of_y(), and then in each of X1...
-
- >...
-
- This doesn't sound quite right. Putting do-nothing member_of_y()
- methods in X because clients of X expect some of Y's methods just
- seems to imply you could do better with a different hierarchy, perhaps
- another abstract class, XwithY. Now, why is it so distateful to
- derive X4 from both X and Y?
-
- If some clients of X expect to find Y methods, then those clients
- might do better to accept a Y reference or some new XwithY.
-
- Clients can also dynamic_cast<Y *>(pX) if your compiler supports
- run-time type information (RTTI). The cast would return null for X
- pointers really pointing to X4s, and would return a Y ptr otherwise.
-
- My two cents.
-
- --Norm
-
-
-